package com.fenghongzhang.day008;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.io.InputStream;

public class Main2Activity extends AppCompatActivity {

    private ImageView img;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AssetManager assets = getAssets();
                try {
                    InputStream open = assets.open("kawayi.jpeg");

                    //把一个流,转换成一个图片
                    Bitmap bitmap = BitmapFactory.decodeStream(open);
                    img.setImageBitmap(bitmap);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initView() {
        img = (ImageView) findViewById(R.id.img);
        btn = (Button) findViewById(R.id.btn);
    }

    public void btn1(View view) {

        //5
        AssetManager assets = getAssets();
        try {
            InputStream open = assets.open("bb/json.txt");

            byte[] bytes = new byte[1024];
            int len = 0;
            String s = null;
            while((len = open.read(bytes)) != -1){
                 s = new String(bytes, 0, len);
            }
            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
